package com.wibo.common;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class FileDbHelper {

	private static final String TAG = "FileDbHelper";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_NAME = "Weibo_file_db";
	
	public static final long MY_WEIBO_LIST = 0;
	public static final long NOT_WEIBO_LIST = 1;
	public static final long MY_COMMENT_LIST = 2;
	public static final long MY_TRANSMIT_LIST = 3;

	// Weibo table
	private static final String WEIBO_LIST = "Weibo_list_talbe";

	// 字段
	public static final String WEIBO_LIST_WEIBO_ID = "id";
	public static final String WEIBO_LIST_USER_PIC = "user_pic";
	public static final String WEIBO_LIST_WEIBO_PIC = "weibo_pic";
	public static final String WEIBO_LIST_WEIBO_MID_PIC = "weibo_mid_pic";
	public static final String WEIBO_LIST_WEIBO_LARGE_PIC = "weibo_large_pic";
	public static final String WEIBO_LIST_ORIGINAL_WEIBO_PIC = "original_weibo_pic";
	public static final String WEIBO_LIST_ROOT_WEIBO_MID_PIC = "root_weibo_mid_pic";
	public static final String WEIBO_LIST_ROOT_WEIBO_LARGE_PIC = "root_weibo_large_pic";
	public static final String WEIBO_LIST_USERNAME = "username";
	public static final String WEIBO_LIST_WEIBO_TEXT = "weibo_text";
	public static final String WEIBO_LIST_ORIGINAL_TEXT = "original_text";
	public static final String WEIBO_LIST_SENT_TIME = "sent_time";
	public static final String WEIBO_LIST_CLIENT_NAME = "client_name";
	public static final String WEIBO_LIST_COMMENT_COUNT = "comment_count";
	public static final String WEIBO_LIST_TRANSMIT_COUNT = "transmit_count";
	
	public static final String WEIBO_LIST_VIP = "vip";
	public static final String WEIBO_LIST_USER_ID = "user_id";
	public static final String WEIBO_LIST_RTROOT_WEIBO_ID = "rtroot_id";
	public static final String WEIBO_LIST_RTROOT_UID = "rtroot_uid";
	public static final String WEIBO_LIST_RTROOT_USERNAME = "rtroot_username";
	public static final String WEIBO_LIST_RTROOT_VIP = "rtroot_vip";
	public static final String WEIBO_LIST_LONGITUDE = "longitude";
	public static final String WEIBO_LIST_LATITUDE = "latitude";
	
	public static final String WEIBO_LIST_ADD_DATA1 = "add_data1";
	public static final String WEIBO_LIST_ADD_DATA2 = "add_data2";

	public static final String[] WEIBO_LIST_COLUMNS = { WEIBO_LIST_WEIBO_ID,
			WEIBO_LIST_USER_PIC, WEIBO_LIST_WEIBO_PIC, WEIBO_LIST_WEIBO_MID_PIC, WEIBO_LIST_WEIBO_LARGE_PIC,
			WEIBO_LIST_ORIGINAL_WEIBO_PIC, WEIBO_LIST_ROOT_WEIBO_MID_PIC, WEIBO_LIST_ROOT_WEIBO_LARGE_PIC, 
			WEIBO_LIST_USERNAME,
			WEIBO_LIST_WEIBO_TEXT, WEIBO_LIST_ORIGINAL_TEXT,
			WEIBO_LIST_SENT_TIME, WEIBO_LIST_CLIENT_NAME,
			WEIBO_LIST_COMMENT_COUNT, WEIBO_LIST_TRANSMIT_COUNT,
			
			WEIBO_LIST_VIP, WEIBO_LIST_USER_ID,
			WEIBO_LIST_RTROOT_WEIBO_ID, WEIBO_LIST_RTROOT_UID,
			WEIBO_LIST_RTROOT_USERNAME, WEIBO_LIST_RTROOT_VIP,
			WEIBO_LIST_LONGITUDE, WEIBO_LIST_LATITUDE,
			
			WEIBO_LIST_ADD_DATA1, WEIBO_LIST_ADD_DATA2 };

	// 创建数据表
	private static final String SQL_CREATE_WEIBO_LIST_TABLE = "create table "
			+ WEIBO_LIST + " ( " + WEIBO_LIST_WEIBO_ID
			+ " INTEGER primary key, " + WEIBO_LIST_USER_PIC + " text, "
			+ WEIBO_LIST_WEIBO_PIC + " text, "+ WEIBO_LIST_WEIBO_MID_PIC + " text, " 
			+ WEIBO_LIST_WEIBO_LARGE_PIC + " text, " 
			+ WEIBO_LIST_ORIGINAL_WEIBO_PIC + " text, " 
			+ WEIBO_LIST_ROOT_WEIBO_MID_PIC + " text, " 
			+ WEIBO_LIST_ROOT_WEIBO_LARGE_PIC + " text, " 
			+ WEIBO_LIST_USERNAME + " text, "
			+ WEIBO_LIST_WEIBO_TEXT + " text, " + WEIBO_LIST_ORIGINAL_TEXT
			+ " text, " + WEIBO_LIST_SENT_TIME + " text, "
			+ WEIBO_LIST_CLIENT_NAME + " text, " + WEIBO_LIST_COMMENT_COUNT
			+ " text, " + WEIBO_LIST_TRANSMIT_COUNT + " text, "
			
			+ WEIBO_LIST_VIP + " text, " + WEIBO_LIST_USER_ID
			+ " INTEGER, " + WEIBO_LIST_RTROOT_WEIBO_ID + " INTEGER, "
			+ WEIBO_LIST_RTROOT_UID + " INTEGER, " 
			+ WEIBO_LIST_RTROOT_USERNAME + " text, " + WEIBO_LIST_RTROOT_VIP
			+ " text, " + WEIBO_LIST_LONGITUDE + " text, "
			+ WEIBO_LIST_LATITUDE + " text, "
			
			+ WEIBO_LIST_ADD_DATA1 + " INTEGER, " + WEIBO_LIST_ADD_DATA2
			+ " text)";
	// 刷新数据表的sql语句
	private static final String SQL_UPGRADE_WEIBO_LIST_TABLE = "DROP TABLE IF EXISTS "
			+ WEIBO_LIST;
	
	//////////////////////////////////////////////////////////////////////////////	
	// comment table
	private static final String COMMENT_LIST = "Comment_list_talbe";

	// 字段
	public static final String COMMENT_LIST_COMMENT_ID = "id";
	public static final String COMMENT_LIST_USER_PIC = "user_pic";
	public static final String COMMENT_LIST_USERNAME = "username";
	public static final String COMMENT_LIST_USER_ID = "user_id";
	public static final String COMMENT_LIST_WEIBO_TEXT = "comment_text";
	public static final String COMMENT_LIST_SENT_TIME = "sent_time";
	public static final String COMMENT_LIST_CLIENT_NAME = "client_name";
	public static final String COMMENT_LIST_VIP = "vip";
	public static final String COMMENT_LIST_ROOT_TEXT = "root_text";
	public static final String COMMENT_LIST_ROOT_ID = "root_weibo_id";
	public static final String COMMENT_LIST_ROOT_USERNAME = "root_username";
	public static final String COMMENT_LIST_LONGITUDE = "longitude";
	public static final String COMMENT_LIST_LATITUDE = "latitude";
	
	public static final String COMMENT_LIST_ADD_DATA1 = "add_data1";
	public static final String COMMENT_LIST_ADD_DATA2 = "add_data2";
	
	public static final String[] COMMENT_LIST_COLUMNS = { COMMENT_LIST_COMMENT_ID,
		COMMENT_LIST_USER_PIC, COMMENT_LIST_USERNAME,
		COMMENT_LIST_USER_ID, COMMENT_LIST_WEIBO_TEXT,
		COMMENT_LIST_SENT_TIME, COMMENT_LIST_CLIENT_NAME,
		COMMENT_LIST_VIP, COMMENT_LIST_ROOT_TEXT,
		COMMENT_LIST_ROOT_ID, COMMENT_LIST_ROOT_USERNAME,
		
		COMMENT_LIST_LONGITUDE, COMMENT_LIST_LATITUDE,
		
		COMMENT_LIST_ADD_DATA1, COMMENT_LIST_ADD_DATA2 };
	
	// 创建数据表
	private static final String SQL_CREATE_COMMENT_LIST_TABLE = "create table "
			+ COMMENT_LIST + " ( " + COMMENT_LIST_COMMENT_ID
			+ " INTEGER primary key, " + COMMENT_LIST_USER_PIC + " text, "
			+ COMMENT_LIST_USERNAME + " text, " + COMMENT_LIST_USER_ID
			+ " INTEGER, " + COMMENT_LIST_WEIBO_TEXT + " text, " + COMMENT_LIST_SENT_TIME + " text, "
			+ COMMENT_LIST_CLIENT_NAME + " text, " + COMMENT_LIST_VIP
			+ " text, " + COMMENT_LIST_ROOT_TEXT + " text, "
			+ COMMENT_LIST_ROOT_ID + " INTEGER, " + COMMENT_LIST_ROOT_USERNAME
			+ " text, "
			
			+ COMMENT_LIST_LONGITUDE + " text, "
			+ COMMENT_LIST_LATITUDE + " text, "
			
			+ COMMENT_LIST_ADD_DATA1 + " INTEGER, " + COMMENT_LIST_ADD_DATA2
			+ " text)";
	// 刷新数据表的sql语句
	private static final String SQL_UPGRADE_COMMENT_LIST_TABLE = "DROP TABLE IF EXISTS "
			+ COMMENT_LIST;
	
//////////////////////////////////////////////////////////////////////////////
	// user table
	private static final String USER_LIST = "User_list_talbe";

	// 字段
	public static final String USER_LIST_USER_ID = "id";
	public static final String USER_LIST_USER_PIC = "user_pic";
	public static final String USER_LIST_USERNAME = "username";
	//public static final String USER_LIST_USER_ID = "user_id";
	//public static final String USER_LIST_WEIBO_TEXT = "comment_text";
	public static final String USER_LIST_CREATE_TIME = "create_time";
	public static final String USER_LIST_URL = "url";
	public static final String USER_LIST_VIP = "vip";
	public static final String USER_LIST_DESCRIPTION = "description";
	
	public static final String USER_LIST_FOLLOWERS_COUNT = "followers_count";
	public static final String USER_LIST_FRIENDS_COUNT = "friends_count";
	public static final String USER_LIST_FAVOURITES_COUNT = "favourites_count";
	public static final String USER_LIST_STATUSES_COUNT = "statuses_count";
	
	public static final String USER_LIST_DOMAIN = "domain";
	public static final String USER_LIST_LOCATION = "location";
	public static final String USER_LIST_SCREEN_NAME = "screen_name";
	
	public static final String USER_LIST_ADD_DATA1 = "add_data1";
	public static final String USER_LIST_ADD_DATA2 = "add_data2";
	
	public static final String[] USER_LIST_COLUMNS = { USER_LIST_USER_ID,
		USER_LIST_USER_PIC, USER_LIST_USERNAME, 
		USER_LIST_CREATE_TIME, USER_LIST_URL,
		USER_LIST_VIP, USER_LIST_DESCRIPTION,
		USER_LIST_FOLLOWERS_COUNT, USER_LIST_FRIENDS_COUNT, 
		USER_LIST_FAVOURITES_COUNT, USER_LIST_STATUSES_COUNT, USER_LIST_DOMAIN,
		
		USER_LIST_LOCATION, USER_LIST_SCREEN_NAME,
		
		USER_LIST_ADD_DATA1, USER_LIST_ADD_DATA2 };
	
	// 创建数据表
	private static final String SQL_CREATE_USER_LIST_TABLE = "create table "
			+ USER_LIST + " ( " + USER_LIST_USER_ID
			+ " INTEGER primary key, " + USER_LIST_USER_PIC + " text, "
			+ USER_LIST_USERNAME + " text, " + USER_LIST_CREATE_TIME + " text, "
			+ USER_LIST_URL + " text, " + USER_LIST_VIP
			+ " text, " + USER_LIST_DESCRIPTION + " text, "
			+ USER_LIST_FOLLOWERS_COUNT + " INTEGER, "
			+ USER_LIST_FRIENDS_COUNT + " INTEGER, "
			+ USER_LIST_FAVOURITES_COUNT + " INTEGER, "
			+ USER_LIST_STATUSES_COUNT + " INTEGER, "+ USER_LIST_DOMAIN
			+ " text, "
			
			+ USER_LIST_LOCATION + " text, "
			+ USER_LIST_SCREEN_NAME + " text, "
			
			+ USER_LIST_ADD_DATA1 + " INTEGER, " + USER_LIST_ADD_DATA2
			+ " text)";
	// 刷新数据表的sql语句
	private static final String SQL_UPGRADE_USER_LIST_TABLE = "DROP TABLE IF EXISTS "
			+ USER_LIST;
	
	// tran table
	private static final String TRANSMIT_LIST = "transmit_list_talbe";
	
	// 创建数据表
	private static final String SQL_CREATE_TRANSMIT_LIST_TABLE = "create table "
			+ TRANSMIT_LIST + " ( " + WEIBO_LIST_WEIBO_ID
			+ " INTEGER primary key, " + WEIBO_LIST_USER_PIC + " text, "
			+ WEIBO_LIST_WEIBO_PIC + " text, "+ WEIBO_LIST_WEIBO_MID_PIC + " text, " 
			+ WEIBO_LIST_WEIBO_LARGE_PIC + " text, " 
			+ WEIBO_LIST_ORIGINAL_WEIBO_PIC + " text, " 
			+ WEIBO_LIST_ROOT_WEIBO_MID_PIC + " text, " 
			+ WEIBO_LIST_ROOT_WEIBO_LARGE_PIC + " text, " 
			+ WEIBO_LIST_USERNAME + " text, "
			+ WEIBO_LIST_WEIBO_TEXT + " text, " + WEIBO_LIST_ORIGINAL_TEXT
			+ " text, " + WEIBO_LIST_SENT_TIME + " text, "
			+ WEIBO_LIST_CLIENT_NAME + " text, " + WEIBO_LIST_COMMENT_COUNT
			+ " text, " + WEIBO_LIST_TRANSMIT_COUNT + " text, "
			
			+ WEIBO_LIST_VIP + " text, " + WEIBO_LIST_USER_ID
			+ " INTEGER, " + WEIBO_LIST_RTROOT_WEIBO_ID + " INTEGER, "
			+ WEIBO_LIST_RTROOT_UID + " INTEGER, " 
			+ WEIBO_LIST_RTROOT_USERNAME + " text, " + WEIBO_LIST_RTROOT_VIP
			+ " text, " + WEIBO_LIST_LONGITUDE + " text, "
			+ WEIBO_LIST_LATITUDE + " text, "
			
			+ WEIBO_LIST_ADD_DATA1 + " INTEGER, " + WEIBO_LIST_ADD_DATA2
			+ " text)";
	// 刷新数据表的sql语句
	private static final String SQL_UPGRADE_TRANSMIT_LIST_TABLE = "DROP TABLE IF EXISTS "
			+ TRANSMIT_LIST;

	private Context mContext = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private MyDatabaseHelper mDatabaseHelper = null;

	private static class MyDatabaseHelper extends SQLiteOpenHelper {

		public MyDatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(SQL_CREATE_WEIBO_LIST_TABLE);
			db.execSQL(SQL_CREATE_COMMENT_LIST_TABLE);
			db.execSQL(SQL_CREATE_USER_LIST_TABLE);
			db.execSQL(SQL_CREATE_TRANSMIT_LIST_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(SQL_UPGRADE_WEIBO_LIST_TABLE);
			db.execSQL(SQL_UPGRADE_COMMENT_LIST_TABLE);
			db.execSQL(SQL_UPGRADE_USER_LIST_TABLE);
			db.execSQL(SQL_UPGRADE_TRANSMIT_LIST_TABLE);
			onCreate(db);
		}
	}

	public FileDbHelper(Context context) {
		mContext = context;
		// 检查数据库
		mDatabaseHelper = new MyDatabaseHelper(context);
		SQLiteDatabase checkDb = mDatabaseHelper.getReadableDatabase();
		try {
			Cursor cur = checkDb.query(WEIBO_LIST, WEIBO_LIST_COLUMNS, null,
					null, null, null, null);
			cur = checkDb.query(COMMENT_LIST, COMMENT_LIST_COLUMNS, null,
					null, null, null, null);
			cur = checkDb.query(USER_LIST, USER_LIST_COLUMNS, null,
					null, null, null, null);
			cur = checkDb.query(TRANSMIT_LIST, WEIBO_LIST_COLUMNS, null,
					null, null, null, null);
			cur.close();

		} catch (Exception e) {

			checkDb.execSQL(SQL_UPGRADE_WEIBO_LIST_TABLE);
			checkDb.execSQL(SQL_CREATE_WEIBO_LIST_TABLE);
			checkDb.execSQL(SQL_UPGRADE_COMMENT_LIST_TABLE);
			checkDb.execSQL(SQL_CREATE_COMMENT_LIST_TABLE);
			checkDb.execSQL(SQL_UPGRADE_USER_LIST_TABLE);
			checkDb.execSQL(SQL_CREATE_USER_LIST_TABLE);
			checkDb.execSQL(SQL_UPGRADE_TRANSMIT_LIST_TABLE);
			checkDb.execSQL(SQL_CREATE_TRANSMIT_LIST_TABLE);
		}

		mDatabaseHelper.close();
		mDatabaseHelper = null;
	}

	// 打开数据库,在使用其所有其他方法前，必须先调用该方法打开
	// 如果该数据库已经打开(打开后没有调用close方法关闭),则不会再次打开
	public void open() throws SQLiteException {
		if (mDatabaseHelper == null || mSQLiteDatabase == null) {
			mDatabaseHelper = new MyDatabaseHelper(mContext);
			mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
		}
	}

	// 调用open之后，使用完，必须使用该方法关闭该数据库
	public void close() {
		if (mDatabaseHelper != null) {
			mDatabaseHelper.close();
			mDatabaseHelper = null;
			mSQLiteDatabase = null;
		}
	}

	// 获取所有数据
	public Cursor getAllWeiboListData() {
		return mSQLiteDatabase.query(WEIBO_LIST, WEIBO_LIST_COLUMNS, null,
				null, null, null, null);
	}

	// 删除指定的数据
	public boolean deleteWeiboListData(long id) {
		return mSQLiteDatabase.delete(WEIBO_LIST, WEIBO_LIST_WEIBO_ID + "="
				+ id, null) > 0;
	}

	public Cursor getWeiboListData(long id) {
		Cursor cursor = mSQLiteDatabase.query(WEIBO_LIST, WEIBO_LIST_COLUMNS,
				WEIBO_LIST_WEIBO_ID + "=" + id/* + " and " + WEIBO_LIST_ADD_DATA1 + "=" + MY_WEIBO_LIST*/, null, null, null, null);
		return cursor;
	}

	public boolean checkWeiboList(long id) {
		Cursor cursor = getWeiboListData(id);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			} else {
				cursor.close();
				return false;
			}
		} else {
			return false;
		}
	}

	public long insertWeiboListData(HashMap<String, Object> hashMap) {

		ContentValues initValues = new ContentValues();
		Long id = (Long) hashMap.get(WEIBO_LIST_WEIBO_ID);
		// String temp = (String) hashMap.get(WEIBO_LIST_WEIBO_ID);
		initValues.put(WEIBO_LIST_WEIBO_ID, id.longValue());// Long.valueOf((String)
															// hashMap.get(WEIBO_LIST_WEIBO_ID))

		initValues.put(WEIBO_LIST_USER_PIC, (String) hashMap.get(WEIBO_LIST_USER_PIC));

		initValues.put(WEIBO_LIST_WEIBO_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_PIC));
		initValues.put(WEIBO_LIST_WEIBO_MID_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_MID_PIC));
		initValues.put(WEIBO_LIST_WEIBO_LARGE_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_LARGE_PIC));
		initValues.put(WEIBO_LIST_ORIGINAL_WEIBO_PIC, (String) hashMap.get(WEIBO_LIST_ORIGINAL_WEIBO_PIC));
		initValues.put(WEIBO_LIST_ROOT_WEIBO_MID_PIC, (String) hashMap.get(WEIBO_LIST_ROOT_WEIBO_MID_PIC));
		initValues.put(WEIBO_LIST_ROOT_WEIBO_LARGE_PIC, (String) hashMap.get(WEIBO_LIST_ROOT_WEIBO_LARGE_PIC));
		initValues.put(WEIBO_LIST_USERNAME, (String) hashMap.get(WEIBO_LIST_USERNAME));
		initValues.put(WEIBO_LIST_WEIBO_TEXT, (String) hashMap.get(WEIBO_LIST_WEIBO_TEXT));
		initValues.put(WEIBO_LIST_ORIGINAL_TEXT, (String) hashMap.get(WEIBO_LIST_ORIGINAL_TEXT));
		initValues.put(WEIBO_LIST_SENT_TIME, (String) hashMap.get(WEIBO_LIST_SENT_TIME));
		initValues.put(WEIBO_LIST_CLIENT_NAME, (String) hashMap.get(WEIBO_LIST_CLIENT_NAME));
		
		initValues.put(WEIBO_LIST_VIP, (String) hashMap.get(WEIBO_LIST_VIP));
		
		id = (Long) hashMap.get(WEIBO_LIST_USER_ID);
		initValues.put(WEIBO_LIST_USER_ID, id.longValue());
		
		id = (Long) hashMap.get(WEIBO_LIST_RTROOT_WEIBO_ID);
		initValues.put(WEIBO_LIST_RTROOT_WEIBO_ID, id.longValue());
		
		id = (Long) hashMap.get(WEIBO_LIST_RTROOT_UID);
		initValues.put(WEIBO_LIST_RTROOT_UID, id.longValue());
		
		initValues.put(WEIBO_LIST_RTROOT_USERNAME, (String) hashMap.get(WEIBO_LIST_RTROOT_USERNAME));
		initValues.put(WEIBO_LIST_RTROOT_VIP, (String) hashMap.get(WEIBO_LIST_RTROOT_VIP));
		initValues.put(WEIBO_LIST_LONGITUDE, (String) hashMap.get(WEIBO_LIST_LONGITUDE));
		initValues.put(WEIBO_LIST_LATITUDE, (String) hashMap.get(WEIBO_LIST_LATITUDE));
		
		id = (Long) hashMap.get(WEIBO_LIST_ADD_DATA1);
		initValues.put(WEIBO_LIST_ADD_DATA1, id.longValue());


		initValues.put(WEIBO_LIST_COMMENT_COUNT, (String) hashMap.get(WEIBO_LIST_COMMENT_COUNT));
//		value = 0;
//		if (hashMap.get(WEIBO_LIST_COMMENT_COUNT) != null) {
//			value = (Integer) hashMap.get(WEIBO_LIST_SENT_TIME);
//		}
		initValues.put(WEIBO_LIST_TRANSMIT_COUNT, 
				 (String) hashMap.get(WEIBO_LIST_TRANSMIT_COUNT));

		// initValues.put(WEIBO_LIST_ADD_DATA1,
		// (String)hashMap.get(WEIBO_LIST_ADD_DATA1));
		// initValues.put(WEIBO_LIST_ADD_DATA2,
		// (String)hashMap.get(WEIBO_LIST_ADD_DATA2));
		id = (Long) hashMap.get(WEIBO_LIST_WEIBO_ID);
		if (checkWeiboList(id.longValue())) { //Long.valueOf((String) hashMap.get(WEIBO_LIST_WEIBO_ID))
			deleteWeiboListData(id.longValue());
		}
		return mSQLiteDatabase.insert(WEIBO_LIST, null, initValues);
	}

	public void deleteAllWeiboListData() {
		SQLiteDatabase checkDb = mDatabaseHelper.getReadableDatabase();
		checkDb.execSQL(SQL_UPGRADE_WEIBO_LIST_TABLE);
		checkDb.execSQL(SQL_CREATE_WEIBO_LIST_TABLE);
		// mSQLiteDatabase.execSQL("TRUNCATE " + WEIBO_LIST);
	}
	
	public long updateCount(long weiboId, String commentCount, String transmitCount){
		ContentValues initValues = new ContentValues();
		initValues.put(WEIBO_LIST_COMMENT_COUNT, commentCount);
		initValues.put(WEIBO_LIST_TRANSMIT_COUNT, transmitCount);
		return mSQLiteDatabase.update(WEIBO_LIST, initValues, WEIBO_LIST_WEIBO_ID + "=" + weiboId, null);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////
	// Comment
	// 获取所有数据
	public Cursor getAllCommentListData() {
		return mSQLiteDatabase.query(COMMENT_LIST, COMMENT_LIST_COLUMNS, null,
				null, null, null, null);
	}

	// 删除指定的数据
	public boolean deleteCommentListData(long id) {
		return mSQLiteDatabase.delete(COMMENT_LIST, COMMENT_LIST_COMMENT_ID + "="
				+ id, null) > 0;
	}

	public Cursor getCommentListData(long id) {
		Cursor cursor = mSQLiteDatabase.query(COMMENT_LIST, COMMENT_LIST_COLUMNS,
				COMMENT_LIST_COMMENT_ID + "=" + id, null, null, null, null);
		return cursor;
	}

	public boolean checkCommentList(long id) {
		Cursor cursor = getCommentListData(id);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			} else {
				cursor.close();
				return false;
			}
		} else {
			return false;
		}
	}

	public long insertCommentListData(HashMap<String, Object> hashMap) {

		ContentValues initValues = new ContentValues();
		Long id = (Long) hashMap.get(COMMENT_LIST_COMMENT_ID);
		initValues.put(COMMENT_LIST_COMMENT_ID, id.longValue());
		
		id = (Long) hashMap.get(COMMENT_LIST_USER_ID);		
		initValues.put(COMMENT_LIST_USER_ID, id.longValue());
		
		initValues.put(COMMENT_LIST_USER_PIC, (String) hashMap.get(COMMENT_LIST_USER_PIC));
		initValues.put(COMMENT_LIST_USERNAME, (String) hashMap.get(COMMENT_LIST_USERNAME));
		initValues.put(COMMENT_LIST_WEIBO_TEXT, (String) hashMap.get(COMMENT_LIST_WEIBO_TEXT));
		initValues.put(COMMENT_LIST_ROOT_TEXT, (String) hashMap.get(COMMENT_LIST_ROOT_TEXT));
		initValues.put(COMMENT_LIST_SENT_TIME, (String) hashMap.get(COMMENT_LIST_SENT_TIME));
		initValues.put(COMMENT_LIST_CLIENT_NAME, (String) hashMap.get(COMMENT_LIST_CLIENT_NAME));
		initValues.put(COMMENT_LIST_VIP, (String) hashMap.get(COMMENT_LIST_VIP));
		
		id = (Long) hashMap.get(COMMENT_LIST_ROOT_ID);
		initValues.put(COMMENT_LIST_ROOT_ID, id.longValue());
		
		initValues.put(COMMENT_LIST_ROOT_USERNAME, (String) hashMap.get(COMMENT_LIST_ROOT_USERNAME));
		initValues.put(COMMENT_LIST_LONGITUDE, (String) hashMap.get(COMMENT_LIST_LONGITUDE));
		initValues.put(COMMENT_LIST_LATITUDE, (String) hashMap.get(COMMENT_LIST_LATITUDE));
		
		id = (Long) hashMap.get(COMMENT_LIST_ADD_DATA1);
		initValues.put(COMMENT_LIST_ADD_DATA1, id.longValue());

		id = (Long) hashMap.get(COMMENT_LIST_COMMENT_ID);
		if (checkCommentList(id.longValue())) {
			deleteCommentListData(id.longValue());
		}
		return mSQLiteDatabase.insert(COMMENT_LIST, null, initValues);
	}

	public void deleteAllCommentListData() {
		SQLiteDatabase checkDb = mDatabaseHelper.getReadableDatabase();
		checkDb.execSQL(SQL_UPGRADE_COMMENT_LIST_TABLE);
		checkDb.execSQL(SQL_CREATE_COMMENT_LIST_TABLE);
	}
	
///////////////////////////////////////////////////////////////////////////////////////
	// User
	// 获取所有数据
	public Cursor getAllUserListData() {
		return mSQLiteDatabase.query(USER_LIST, USER_LIST_COLUMNS, null,
				null, null, null, null);
	}

	// 删除指定的数据
	public boolean deleteUserListData(long id) {
		return mSQLiteDatabase.delete(USER_LIST, USER_LIST_USER_ID + "="
				+ id, null) > 0;
	}

	public Cursor getUserListData(long id) {
		Cursor cursor = mSQLiteDatabase.query(USER_LIST, USER_LIST_COLUMNS,
				USER_LIST_USER_ID + "=" + id, null, null, null, null);
		return cursor;
	}

	public boolean checkUserList(long id) {
		Cursor cursor = getUserListData(id);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			} else {
				cursor.close();
				return false;
			}
		} else {
			return false;
		}
	}

	public long insertUserListData(HashMap<String, Object> hashMap) {

		ContentValues initValues = new ContentValues();
		Long id = (Long) hashMap.get(USER_LIST_USER_ID);
		initValues.put(USER_LIST_USER_ID, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FOLLOWERS_COUNT);		
		initValues.put(USER_LIST_FOLLOWERS_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FRIENDS_COUNT);		
		initValues.put(USER_LIST_FRIENDS_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FAVOURITES_COUNT);		
		initValues.put(USER_LIST_FAVOURITES_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_STATUSES_COUNT);		
		initValues.put(USER_LIST_STATUSES_COUNT, id.longValue());
		
		initValues.put(USER_LIST_USER_PIC, (String) hashMap.get(USER_LIST_USER_PIC));
		initValues.put(USER_LIST_USERNAME, (String) hashMap.get(USER_LIST_USERNAME));
		initValues.put(USER_LIST_URL, (String) hashMap.get(USER_LIST_URL));
		initValues.put(USER_LIST_DESCRIPTION, (String) hashMap.get(USER_LIST_DESCRIPTION));
		initValues.put(USER_LIST_CREATE_TIME, (String) hashMap.get(USER_LIST_CREATE_TIME));
		initValues.put(USER_LIST_SCREEN_NAME, (String) hashMap.get(USER_LIST_SCREEN_NAME));
		initValues.put(USER_LIST_VIP, (String) hashMap.get(USER_LIST_VIP));

		initValues.put(USER_LIST_DOMAIN, (String) hashMap.get(USER_LIST_DOMAIN));
		initValues.put(USER_LIST_LOCATION, (String) hashMap.get(USER_LIST_LOCATION));
		
		id = (Long) hashMap.get(USER_LIST_ADD_DATA1);
		initValues.put(USER_LIST_ADD_DATA1, id.longValue());

		initValues.put(USER_LIST_ADD_DATA2, (String) hashMap.get(USER_LIST_ADD_DATA2));

		id = (Long) hashMap.get(USER_LIST_USER_ID);
		if (checkUserList(id.longValue())) {
			deleteUserListData(id.longValue());
		}
		return mSQLiteDatabase.insert(USER_LIST, null, initValues);
	}
	
	public int updateUserListData(HashMap<String, Object> hashMap) {
		ContentValues initValues = new ContentValues();
		Long id = (Long) hashMap.get(USER_LIST_USER_ID);
		initValues.put(USER_LIST_USER_ID, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FOLLOWERS_COUNT);		
		initValues.put(USER_LIST_FOLLOWERS_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FRIENDS_COUNT);		
		initValues.put(USER_LIST_FRIENDS_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_FAVOURITES_COUNT);		
		initValues.put(USER_LIST_FAVOURITES_COUNT, id.longValue());
		
		id = (Long) hashMap.get(USER_LIST_STATUSES_COUNT);		
		initValues.put(USER_LIST_STATUSES_COUNT, id.longValue());
		
		initValues.put(USER_LIST_USER_PIC, (String) hashMap.get(USER_LIST_USER_PIC));
		initValues.put(USER_LIST_USERNAME, (String) hashMap.get(USER_LIST_USERNAME));
		initValues.put(USER_LIST_URL, (String) hashMap.get(USER_LIST_URL));
		initValues.put(USER_LIST_DESCRIPTION, (String) hashMap.get(USER_LIST_DESCRIPTION));
		initValues.put(USER_LIST_CREATE_TIME, (String) hashMap.get(USER_LIST_CREATE_TIME));
		initValues.put(USER_LIST_SCREEN_NAME, (String) hashMap.get(USER_LIST_SCREEN_NAME));
		initValues.put(USER_LIST_VIP, (String) hashMap.get(USER_LIST_VIP));

		initValues.put(USER_LIST_DOMAIN, (String) hashMap.get(USER_LIST_DOMAIN));
		initValues.put(USER_LIST_LOCATION, (String) hashMap.get(USER_LIST_LOCATION));
		
		id = (Long) hashMap.get(USER_LIST_ADD_DATA1);
		initValues.put(USER_LIST_ADD_DATA1, id.longValue());
		
		initValues.put(USER_LIST_ADD_DATA2, (String) hashMap.get(USER_LIST_ADD_DATA2));
		
		return mSQLiteDatabase.update(USER_LIST, initValues, USER_LIST_USER_ID + "="
				+ (Long) hashMap.get(USER_LIST_USER_ID), null);
	}

	public void deleteAllUserListData() {
		SQLiteDatabase checkDb = mDatabaseHelper.getReadableDatabase();
		checkDb.execSQL(SQL_UPGRADE_USER_LIST_TABLE);
		checkDb.execSQL(SQL_CREATE_USER_LIST_TABLE);
	}
	
	// transmit
	/////////////////////////
	
	// 获取所有数据
	public Cursor getAllTransmitListData() {
		return mSQLiteDatabase.query(TRANSMIT_LIST, WEIBO_LIST_COLUMNS, null,
				null, null, null, null);
	}

	// 删除指定的数据
	public boolean deleteTransmitListData(long id) {
		return mSQLiteDatabase.delete(TRANSMIT_LIST, WEIBO_LIST_WEIBO_ID + "="
				+ id, null) > 0;
	}

	public Cursor getTransmitListData(long id) {
		Cursor cursor = mSQLiteDatabase.query(TRANSMIT_LIST, WEIBO_LIST_COLUMNS,
				WEIBO_LIST_WEIBO_ID + "=" + id, null, null, null, null);
		return cursor;
	}

	public boolean checkTransmitList(long id) {
		Cursor cursor = getTransmitListData(id);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			} else {
				cursor.close();
				return false;
			}
		} else {
			return false;
		}
	}

	public long insertTransmitListData(HashMap<String, Object> hashMap) {

		ContentValues initValues = new ContentValues();
		Long id = (Long) hashMap.get(WEIBO_LIST_WEIBO_ID);
		// String temp = (String) hashMap.get(WEIBO_LIST_WEIBO_ID);
		initValues.put(WEIBO_LIST_WEIBO_ID, id.longValue());// Long.valueOf((String)
															// hashMap.get(WEIBO_LIST_WEIBO_ID))

		initValues.put(WEIBO_LIST_USER_PIC, (String) hashMap.get(WEIBO_LIST_USER_PIC));

		initValues.put(WEIBO_LIST_WEIBO_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_PIC));
		initValues.put(WEIBO_LIST_WEIBO_MID_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_MID_PIC));
		initValues.put(WEIBO_LIST_WEIBO_LARGE_PIC, (String) hashMap.get(WEIBO_LIST_WEIBO_LARGE_PIC));
		initValues.put(WEIBO_LIST_ORIGINAL_WEIBO_PIC, (String) hashMap.get(WEIBO_LIST_ORIGINAL_WEIBO_PIC));
		initValues.put(WEIBO_LIST_ROOT_WEIBO_MID_PIC, (String) hashMap.get(WEIBO_LIST_ROOT_WEIBO_MID_PIC));
		initValues.put(WEIBO_LIST_ROOT_WEIBO_LARGE_PIC, (String) hashMap.get(WEIBO_LIST_ROOT_WEIBO_LARGE_PIC));
		initValues.put(WEIBO_LIST_USERNAME, (String) hashMap.get(WEIBO_LIST_USERNAME));
		initValues.put(WEIBO_LIST_WEIBO_TEXT, (String) hashMap.get(WEIBO_LIST_WEIBO_TEXT));
		initValues.put(WEIBO_LIST_ORIGINAL_TEXT, (String) hashMap.get(WEIBO_LIST_ORIGINAL_TEXT));
		initValues.put(WEIBO_LIST_SENT_TIME, (String) hashMap.get(WEIBO_LIST_SENT_TIME));
		initValues.put(WEIBO_LIST_CLIENT_NAME, (String) hashMap.get(WEIBO_LIST_CLIENT_NAME));
		
		initValues.put(WEIBO_LIST_VIP, (String) hashMap.get(WEIBO_LIST_VIP));
		
		id = (Long) hashMap.get(WEIBO_LIST_USER_ID);
		initValues.put(WEIBO_LIST_USER_ID, id.longValue());
		
		id = (Long) hashMap.get(WEIBO_LIST_RTROOT_WEIBO_ID);
		initValues.put(WEIBO_LIST_RTROOT_WEIBO_ID, id.longValue());
		
		id = (Long) hashMap.get(WEIBO_LIST_RTROOT_UID);
		initValues.put(WEIBO_LIST_RTROOT_UID, id.longValue());
		
		initValues.put(WEIBO_LIST_RTROOT_USERNAME, (String) hashMap.get(WEIBO_LIST_RTROOT_USERNAME));
		initValues.put(WEIBO_LIST_RTROOT_VIP, (String) hashMap.get(WEIBO_LIST_RTROOT_VIP));
		initValues.put(WEIBO_LIST_LONGITUDE, (String) hashMap.get(WEIBO_LIST_LONGITUDE));
		initValues.put(WEIBO_LIST_LATITUDE, (String) hashMap.get(WEIBO_LIST_LATITUDE));
		
		id = (Long) hashMap.get(WEIBO_LIST_ADD_DATA1);
		initValues.put(WEIBO_LIST_ADD_DATA1, id.longValue());


		initValues.put(WEIBO_LIST_COMMENT_COUNT, (String) hashMap.get(WEIBO_LIST_COMMENT_COUNT));
//		value = 0;
//		if (hashMap.get(WEIBO_LIST_COMMENT_COUNT) != null) {
//			value = (Integer) hashMap.get(WEIBO_LIST_SENT_TIME);
//		}
		initValues.put(WEIBO_LIST_TRANSMIT_COUNT, 
				 (String) hashMap.get(WEIBO_LIST_TRANSMIT_COUNT));

		// initValues.put(WEIBO_LIST_ADD_DATA1,
		// (String)hashMap.get(WEIBO_LIST_ADD_DATA1));
		// initValues.put(WEIBO_LIST_ADD_DATA2,
		// (String)hashMap.get(WEIBO_LIST_ADD_DATA2));

		id = (Long) hashMap.get(WEIBO_LIST_WEIBO_ID);
		if (checkTransmitList(id.longValue())) { //Long.valueOf((String) hashMap.get(WEIBO_LIST_WEIBO_ID))
			deleteTransmitListData(id.longValue());
		}
		return mSQLiteDatabase.insert(TRANSMIT_LIST, null, initValues);
	}

	public void deleteAllTransmitListData() {
		SQLiteDatabase checkDb = mDatabaseHelper.getReadableDatabase();
		checkDb.execSQL(SQL_UPGRADE_TRANSMIT_LIST_TABLE);
		checkDb.execSQL(SQL_CREATE_TRANSMIT_LIST_TABLE);
		// mSQLiteDatabase.execSQL("TRUNCATE " + WEIBO_LIST);
	}
	
	public long updateTransmitCount(long weiboId, String commentCount, String transmitCount){
		ContentValues initValues = new ContentValues();
		initValues.put(WEIBO_LIST_COMMENT_COUNT, commentCount);
		initValues.put(WEIBO_LIST_TRANSMIT_COUNT, transmitCount);
		return mSQLiteDatabase.update(TRANSMIT_LIST, initValues, WEIBO_LIST_WEIBO_ID + "=" + weiboId, null);
	}
	
}